package net.wanho.customerManage.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.customerManage.dto.AdvancedLogDto;
import net.wanho.customerManage.query.BusinessQuery;
import net.wanho.customerManage.service.BusinessService;
import net.wanho.customerManage.vo.BusinessVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.lang3.StringUtils;

@WebServlet("/BuinessServlet")
public class BuinessServlet extends HttpServlet {
	private BusinessService businessService = BeanFactory.getBean(BusinessService.class) ;
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String methodName = request.getParameter("method");
		try {
			Method method = getClass().getDeclaredMethod(methodName,
					HttpServletRequest.class, HttpServletResponse.class);
			method.setAccessible(true);
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	/*
	 * 新增
	 */
	protected void addBusiness(HttpServletRequest request,HttpServletResponse response) throws IOException {

	}
	/*
	 * 查询
	 */
	protected void getAllBusiness(HttpServletRequest request,HttpServletResponse response) {

		try {
			String strPageNum = request.getParameter("pageNo");
			String strPageSize = request.getParameter("pageSize");
			int pageNum = 1;
			int pageSize = 3 ;
			if (StringUtils.isNotEmpty(strPageNum)){
				pageNum = Integer.parseInt(strPageNum) ;
			}
			if (StringUtils.isNotEmpty(strPageSize)){
				pageSize = Integer.parseInt(strPageSize) ;
			}
			BusinessQuery businessQuery = new BusinessQuery();
			PageInfo<BusinessVo> businessVoPageInfo = businessService.selectCurrPage(pageNum, pageSize, businessQuery);
			request.setAttribute("businessVoPageInfo",businessVoPageInfo);
			request.getRequestDispatcher("/WEB-INF/view/customerManage/business/index.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}
	/*
	 * 查看
	 */
	protected void selectByPrimaryKey(HttpServletRequest request,HttpServletResponse response) {
		try {
		
				request.getRequestDispatcher("/WEB-INF/view/customerManage/business/update.jsp").forward(request, response);
			
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 查询联系人
	 */
	protected void getAllLinkMan(HttpServletRequest request,HttpServletResponse response) {

		try {
			request.getRequestDispatcher("/WEB-INF/view/customerManage/business/create.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 修改
	 */
	protected void updateBuiness(HttpServletRequest request,HttpServletResponse response) throws IOException {
		JSONObject obj=new JSONObject();
		obj.put("flag", 1);
		obj.put("msg", "添加成功");
		response.getWriter().write(obj.toString());
	}
	
	/*
	 * 搜索
	 */
	protected void getSomeBuiness(HttpServletRequest request,HttpServletResponse response) {
		try {
			request.getRequestDispatcher("/WEB-INF/view/customerManage/business/index.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 跳转到 新增 商机 推进界面
 	 * @param request
	 * @param response
	 */
	protected void toAddAdvancedLogView(HttpServletRequest request,HttpServletResponse response) {
		try {
			request.getRequestDispatcher("/WEB-INF/view/customerManage/business/createTj.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/*
	 * 修改
	 */
	protected void addAdvancedLog(HttpServletRequest request,HttpServletResponse response) throws IOException, InvocationTargetException, IllegalAccessException, ServiceException {

			ConvertUtils.register(new Converter() {//注册一个日期转换器

				public Object convert(Class type, Object value) {
					Date date1 =null;
					if(value instanceof String){
						String date = (String) value;
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						try {
							date1 = sdf.parse(date);
						} catch (ParseException e) {
							e.printStackTrace();
						}
					}
					return date1;
				}
			}, Date.class);
		JSONObject obj=new JSONObject();
		AdvancedLogDto advancedLogDto = new AdvancedLogDto();
		BeanUtils.populate(advancedLogDto,request.getParameterMap());
		businessService.insertAdvancedLog(advancedLogDto) ;
		obj.put("flag", 1);
		obj.put("msg", "添加成功");
		response.getWriter().write(obj.toString());
	}
}
